package com.nation.controller;

import com.nation.domain.VRecorderManage;
import com.nation.domain.VSocialAndCondition;
import com.nation.service.RecorderManageService;
import com.nation.service.SocietyService;
import com.nation.utils.CipherUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * Created by dqd on 2017/11/4.
 */
@Controller
@RequestMapping(value = "/RecorderManage")
public class RecorderManageController {
    private static final String []rand={"0","1","2","3","4","5","6","7","8","9"
            ,"a","b","c","d","e","f","g","h","i","j","k"
            ,"l","m","n","o","p","q","r","s","t","u","v"
            ,"w","x","y","z","A","B","C","D","E","F","G"
            ,"H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    @Autowired
    private RecorderManageService RecorderManageService;

    /**
     * 录入员管理
     * @param map
     * @return
     */
    @RequestMapping(value = "/recorder_manage")
    public String development(ModelMap map){
        List<VRecorderManage> rlist=RecorderManageService.findAll();
        map.put("rlist",rlist);
        return "RecorderManage/recorder_manage";
    }

    /**
     * 修改页面
     * @param country_code 代表用户账户
     * @param map
     * @return
     */
    @RequestMapping(value = "/recorder_manage_update")
    public String development_update(String country_code,ModelMap map){
        VRecorderManage recorder=RecorderManageService.findByCountryCode(country_code);
        map.put("recorder",recorder);
        return "RecorderManage/recorder_manage-update";
    }

    /**
     * 修改信息
     * @param VRecorderManage
     * @return
     */
    @RequestMapping(value = "/recorder_manage_update.do")
    @ResponseBody
    public String development_pupdate_do(VRecorderManage VRecorderManage){
        Boolean judge=RecorderManageService.update(VRecorderManage);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }

    /**
     * 删除录入员
     * @param YHZH 用户账户
     * @return
     */
    @RequestMapping(value = "/recorder_manage_delete")
    @ResponseBody
    public String deleteRecorder(String YHZH){
        Boolean judge=RecorderManageService.deleteRecoder(YHZH);
        if(judge)
            return "删除成功";
        else
            return "删除失败";
    }

    /**
     * 添加录入员页面
     * @return
     */
    @RequestMapping(value = "/recorder_manage_add")
    public String insertRecorder(){
        return "RecorderManage/recorder_manage-add";
    }

    /**
     * 添加录入员
     * @param VRecorderManage
     * @return
     */
    @RequestMapping(value = "/recorder_manage_add.do")
    @ResponseBody
    public String recorder_manage_add_do(VRecorderManage VRecorderManage){
        String Salt="";

        for(int i=0;i<6;i++){
            Salt+=rand[(int)(Math.random()*62)+1];
        }
        int s=RecorderManageService.countAllManager();
        String YHZH=String.format("%03d",s+1);
        String YHMM= CipherUtil.generatePassword("123456",Salt);
        VRecorderManage.setYHZH(YHZH);
        VRecorderManage.setSalt(Salt);
        VRecorderManage.setYHMM(YHMM);
        VRecorderManage.setBanji(VRecorderManage.getBanji()+"班");
        VRecorderManage.setGrade(VRecorderManage.getGrade()+"级");
        VRecorderManage.setJSDM("1");
        Boolean judge=RecorderManageService.insertRecoder(VRecorderManage);
        if(judge)
            return "添加成功 初始化密码为123456";
        else
            return "添加失败";
    }

    /**
     * 修改密码页面
     * @param YHZH 用户账户
     * @param map
     * @return
     */
    @RequestMapping(value = "/password_update")
    public String password_update(String YHZH,ModelMap map){
        map.put("YHZH",YHZH);
        return "RecorderManage/password-update";
    }

    /**
     * 修改密码
     * @param VRecorderManage
     * @return
     */
    @RequestMapping(value = "/password_update.do")
    @ResponseBody
    public String password_update_do(VRecorderManage VRecorderManage){
        String Salt="";
        for(int i=0;i<6;i++){
            Salt+=rand[(int)(Math.random()*62)+1];
        }
        String YHMM= CipherUtil.generatePassword(VRecorderManage.getYHMM(),Salt);
        VRecorderManage.setYHMM(YHMM);
        VRecorderManage.setSalt(Salt);
        Boolean judge=RecorderManageService.update(VRecorderManage);
        if(judge)
            return "修改成功";
        else
            return "修改失败";
    }
    @RequestMapping(value="/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException, InvalidFormatException {
        System.out.println(file.getOriginalFilename()+"-+-"+file.getSize());
        Workbook wb= WorkbookFactory.create(file.getInputStream());
        Sheet sheet=wb.getSheetAt(0);
        int last=sheet.getLastRowNum();

        for(int i=1;i<=last;i++){
            VRecorderManage VRecorderManage=new VRecorderManage();
            VRecorderManage.setGrade("2017");
            VRecorderManage.setJSDM("1");
            VRecorderManage.setBanji("1");
            VRecorderManage.setCollege("国际关系");
            VRecorderManage.setSpeciality("国级事务与国际关系");
            sheet.getRow(i).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            sheet.getRow(i).getCell(0).setCellType(Cell.CELL_TYPE_STRING);
            VRecorderManage.setName(sheet.getRow(i).getCell(1).getStringCellValue());
            String YHZH=sheet.getRow(i).getCell(0).getStringCellValue();
            YHZH=String.format("%03d",Integer.parseInt(YHZH));
            VRecorderManage.setYHZH(YHZH);
            String Salt="";
            for(int j=0;j<6;j++){
                Salt+=rand[(int)(Math.random()*61)+1];
            }
            String YHMM= CipherUtil.generatePassword("123456",Salt);
            VRecorderManage.setYHMM(YHMM);
            VRecorderManage.setSalt(Salt);
            RecorderManageService.insertRecoder(VRecorderManage);
        }
        return "s";
    }
}
